FORT = gfortran
MPIFORT = mpif90
FLAGS = -O3 # -Wall
FFLAGS = -fdefault-double-8 -fdefault-real-8
CUCFLAGS = -arch sm_20
CUFFLAGS = -L /usr/local/cuda/lib -lcudart -lstdc++
#CUFFLAGS = -L /home/software/rhel6/cuda/4.2/cuda/lib64 -lcudart -lstdc++
NVCCPATH = $(shell which nvcc)
NPROC = $(shell nproc)

all: mc-retrace mc-retrace-mpi mc-retrace-cuda

mc-retrace: main.f90
	$(FORT) main.f90 -o mc-retrace $(FLAGS) $(FFLAGS)

mc-retrace-mpi: main-mpi.f90
	$(MPIFORT) main-mpi.f90 -o mc-retrace-mpi $(FLAGS) $(FFLAGS)

mc-retrace-cuda: main-cuda.f90 retrace.o
ifneq ($(NVCCPATH),)
	$(FORT) main-cuda.f90 retrace.o -o mc-retrace-cuda $(FLAGS) $(FFLAGS) $(CUFFLAGS)
endif

retrace.o: retrace.cu
ifeq ($(NVCCPATH),)
	@echo "CUDA C compiler nvcc not in path, skipping."
else
	nvcc retrace.cu -c $(FLAGS) $(CUCFLAGS)
endif

###############################################################################

test: test-serial test-mpi test-cuda

test-serial: mc-retrace
	./mc-retrace

test-mpi: mc-retrace-mpi
	mpirun -np $(NPROC) ./mc-retrace-mpi

test-cuda: mc-retrace-cuda
	./mc-retrace-cuda

###############################################################################

clean:
	-rm mc-retrace mc-retrace-mpi mc-retrace-cuda retrace.o
